package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.dao.StaffTypeDAO;
import com.mbc.rmt.data.StaffTypeDataList;
import com.mbc.rmt.entities.StaffType;
import com.mbc.rmt.entities.StaffTypeRpt;


public class StaffTypeMgr extends AbstractMgr {
	private StaffTypeDAO stafftypeDAO;

	public StaffTypeMgr() {
	}

	public StaffTypeMgr(String sessionId) {
		this.sessionId = sessionId;
	}

	public void create(StaffType entity) throws Exception {
		openConnection();
		try {
			stafftypeDAO = RemittanceDAOFactory.getStaffTypeDAO(dbManager
					.getConnection());
			entity.setStafftypeId(KeyGenerator.generateKey(getConnection()));
			stafftypeDAO.create(entity);
		} catch (SQLException e) {
			if (DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e
					.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}

	public void update(StaffType entity) throws Exception {
		openConnection();
		try {
			stafftypeDAO = RemittanceDAOFactory.getStaffTypeDAO(dbManager
					.getConnection());
			stafftypeDAO.update(entity);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public void delete(long key) throws Exception {
		openConnection();
		try {
			stafftypeDAO = RemittanceDAOFactory.getStaffTypeDAO(dbManager
					.getConnection());
			stafftypeDAO.delete(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public StaffType find(long key) throws Exception {
		openConnection();
		try {
			stafftypeDAO = RemittanceDAOFactory.getStaffTypeDAO(dbManager
					.getConnection());
			return stafftypeDAO.find(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public StaffType findByCode(String code,long branchId) throws Exception {
		openConnection();
		try {
			stafftypeDAO = RemittanceDAOFactory.getStaffTypeDAO(dbManager
					.getConnection());
			return stafftypeDAO.findByCode(code,branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public List<StaffType> findAll() throws Exception {
		openConnection();
		try {
			stafftypeDAO = RemittanceDAOFactory.getStaffTypeDAO(dbManager
					.getConnection());
			return stafftypeDAO.findAll();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public StaffTypeDataList findStaffTypes(CriteriaParams cri)
			throws Exception {
		openConnection();
		try {
			stafftypeDAO = RemittanceDAOFactory.getStaffTypeDAO(dbManager
					.getConnection());
			return stafftypeDAO.findStaffTypes(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public StaffTypeDataList getStaffTypeList(StaffTypeRpt info)
			throws Exception {
		openConnection();
		try {
			stafftypeDAO = RemittanceDAOFactory.getStaffTypeDAO(dbManager
					.getConnection());
			return stafftypeDAO.getStaffTypeList(info);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public JasperPrint prepareStaffTypeReport(StaffTypeRpt info)
			throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			stafftypeDAO = RemittanceDAOFactory.getStaffTypeDAO(dbManager
					.getConnection());

			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(
					stafftypeDAO.prepareStaffTypeReport(info));

			JasperReport l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH
							+ "reports/StaffType.jasper");
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("company", "MBC");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
}
